<template>
  <el-dialog
    :visible.sync="isShow"
    width="600px"
    class="register"
    :show-close="true"
  >
    <!-- 表单验证
  1:el-form  model   rules   
  2:在需要验证的项el-form-item上加一个prop="需要验证的字段名"
  3:写验证规则 
  rules:{
    prop的值:[
    {required:true,message:错误信息,trigger:"change/blur"},
    {min:最小长度,max:最大长度,message:错误信息,trigger:"change/blur"}
    ]
  }
   -->
    <div slot="title" class="title">用户注册</div>
    <el-form :model="form" :rules="rules" label-width="70px" ref="form">
      <el-form-item prop="avatar" label="头像">
        <el-upload
          class="avatar-uploader"
          :action="baseUrl + '/uploads'"
          name="image"
          :show-file-list="false"
          :before-upload="beforeUpload"
          :on-success="uploadSuccess"
        >
          <img
            v-if="form.avatar"
            :src="baseUrl + '/' + form.avatar"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item prop="username" label="昵称">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item prop="email" label="邮箱">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item prop="phone" label="手机">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item prop="rcode" label="图形码">
        <el-row>
          <el-col :span="15">
            <el-input v-model="form.rcode"></el-input>
          </el-col>
          <el-col :span="8" :offset="1">
            <img
              class="register-code"
              :src="baseUrl+'/captcha?type=login' "
            />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="验证码">
        <el-row>
          <el-col :span="15">
            <el-input></el-input>
          </el-col>
          <el-col :span="8" :offset="1">
            <el-button>获取用户验证码</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <div slot="footer" class="footer">
      <el-button @click="isShow = false">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  /*watch
  侦听器：就是对某个值（通过this访问的值）的change事件
  定义:  watch:{
    本质就是一个function
    方法名(){
      方法名:写出需要侦听的值,去掉this,加上引号  "isShow"
    }
    "$route.query.参数"(newVal,oldVal){
     newVal:当前值
     oldVal:上一刻的值
    }
    
  } 
  
  */

  watch: {
    "isShow"(newVal){
      if(newVal==false){
        // 清空表单
        this.$refs.form.resetFields()
      }
    }
  },
  data () {
    return {
      baseUrl: process.env.VUE_APP_URL,
      isShow: false,
      // imageUrl: '',
      form: {
        avatar: '', //是	string	头像地址
        username: '', //是	string	用户名
        email: '', //是	string	邮箱
        phone: '', //是	string	手机号
        password: '', //是	string	密码
        rcode: '' //是	string	验证码
      },
      rules: {
        avatar: [{ required: true, message: '请上传头像', trigger: 'change' }],
        username: [
          { required: true, message: '请输入用户名', trigger: 'change' }
        ],
        email: [{ required: true, message: '请输入邮箱', trigger: 'change' },
        
        {validator:(rule,value,callback)=>{
          let _reg=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
          if(_reg.test(value)){
            callback()
          }else{
            callback(new Error('请正确输入邮箱地址'))
          }
        },trigger:"change"
        }
        
        
        ],
        phone: [{ required: true, message: '请输入手机号', trigger: 'change' },
          {validator:(rule,value,callback)=>{
          let _reg=/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
          if(_reg.test(value)){
            callback()
          }else{
            callback(new Error('请正确输入手机号'))
          }
        },trigger:"change"
        }
        
        
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ],
        rcode: [{ required: true, message: '请输入验证码', trigger: 'change' }]
      }
    }
  },
  methods: {
    uploadSuccess (res) {
      this.form.avatar = res.data.file_path
      // 上传成功后同，由于该组件内部没有执行它的表单验证，所以我们要人为触发
      // 父传子（父调用子方法）1：在子组件标签定义ref  2:拿到el-form的this===this.$refs.ref值
      // this.imageUrl = res.data.file_path
      this.$refs.form.validateField(['avatar'])
    },
    beforeUpload (file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 确定按钮点击
    submit () {
      this.$refs.form.validate(result => {
        if (result) {
          this.$message.success('验证成功')
        } else {
          this.$message.error('验证失败')
        }
      })
    }
  }
}
</script>
<style lang="less">
.register {
  .title {
    height: 53px;
    line-height: 53px;
    color: #fff;
    text-align: center;
    background: linear-gradient(to right, #02c4fa, #1394fa);
  }
  .el-dialog__header {
    padding: 0;
  }
  .footer {
    text-align: center;
  }
  .avatar-uploader {
    text-align: center;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .register-code {
    width: 100%;
    height: 40px;
  }
}
</style>
